package cn.maszy.service.impl.projectmanagement;

import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.maszy.common.DateUtils;
import cn.maszy.common.Pager;
import cn.maszy.common.SessionFactory;
import cn.maszy.dao.projectmanagement.B_T_Ufp_TaskHandoutMapper;
import cn.maszy.model.projectmanagement.B_T_Ufp_TaskHandout;
import cn.maszy.model.projectmanagement.B_T_Ufp_TaskHandoutExample;
import cn.maszy.model.projectmanagement.B_T_Ufp_TaskHandoutExample.Criteria;
import cn.maszy.service.projectmanagement.B_T_Ufp_TaskHandoutService;
@Service
public class B_T_Ufp_TaskHandoutServiceImpl implements B_T_Ufp_TaskHandoutService {

	@Autowired
	private B_T_Ufp_TaskHandoutMapper b_T_Ufp_TaskHandoutMapper;
	
	public int update(B_T_Ufp_TaskHandout b_T_Ufp_TaskHandout) {
		// TODO Auto-generated method stub
		 if(b_T_Ufp_TaskHandout.getTaskhandId() == null || "".equals(b_T_Ufp_TaskHandout.getTaskhandId())){
			 b_T_Ufp_TaskHandout.setTaskhandId(UUID.randomUUID().toString());
			 b_T_Ufp_TaskHandout.setIsDelete(0);
			 b_T_Ufp_TaskHandout.setCreateTime(new Date());
			 b_T_Ufp_TaskHandout.setCreateUser(SessionFactory.getSessionUserRealNam());
			 b_T_Ufp_TaskHandout.setAcceptedTime(DateUtils.now());
			 int i= b_T_Ufp_TaskHandoutMapper.insert(b_T_Ufp_TaskHandout);
			 return i;
		 }
		 else
		 {
			 	b_T_Ufp_TaskHandout.setUpdateTime(new Date());
				b_T_Ufp_TaskHandout.setUpdateUser(SessionFactory.getSessionUserRealNam());
				b_T_Ufp_TaskHandout.setAcceptedTime(DateUtils.now());
			 int i= b_T_Ufp_TaskHandoutMapper.updateByPrimaryKeySelective(b_T_Ufp_TaskHandout);
			 return i;
		 }
	}

	public B_T_Ufp_TaskHandout selectOne(String staffId) {
		// TODO Auto-generated method stub
		B_T_Ufp_TaskHandout record=b_T_Ufp_TaskHandoutMapper.selectByPrimaryKey(staffId);
		if("".equals(staffId)){
			record = new B_T_Ufp_TaskHandout();
		}
		return record;
	}

	public Pager<B_T_Ufp_TaskHandout> selectList(B_T_Ufp_TaskHandout b_T_Ufp_TaskHandout,Pager<B_T_Ufp_TaskHandout> pager,String strattime,String endtime,String flag,String taskid) {
		// TODO Auto-generated method stub
		B_T_Ufp_TaskHandoutExample example=new B_T_Ufp_TaskHandoutExample();
		Criteria criteria = example.createCriteria();
			if(b_T_Ufp_TaskHandout!=null){
			if(b_T_Ufp_TaskHandout.getTaskName()!=null && !"".equals(b_T_Ufp_TaskHandout.getTaskName())){
				criteria.andTaskNameLike("%"+b_T_Ufp_TaskHandout.getTaskName()+"%");
			}
			if(strattime!=null&&!"".equals(strattime)){
				criteria.andAcceptedTimeGreaterThanOrEqualTo(strattime);
				}
			if(endtime!=null&&!"".equals(endtime)){
				criteria.andAcceptedTimeLessThanOrEqualTo(endtime);
				}
		}
		if(!"8".equals(flag)){
				criteria.andUserNameEqualTo(SessionFactory.getSessionUserRealNam());
				//criteria.andAcceptedTimeEqualTo(DateUtils.now());
		}
		criteria.andTaskidEqualTo(taskid);
		criteria.andIsDeleteEqualTo(0);
		example.or(criteria);
		int totalCount=new B_T_Ufp_TaskHandoutServiceImpl().count(example,b_T_Ufp_TaskHandoutMapper);
		pager.setTotalRecords(totalCount);
		example.setLimitStart(pager.getStartRecord());
		example.setLimitEnd(pager.getPageSize());
		if("create_time".equals(pager.getOrderField())){
			pager.setOrderField("Accepted_time");
		}
		example.setOrderByClause(pager.getOrderField()+" "+pager.getOrderDirection());
		
		// 取得总的记录条数
		List<B_T_Ufp_TaskHandout>  list=b_T_Ufp_TaskHandoutMapper.selectByExample(example);
		pager.setPageRecords(list);
		return pager;
	}

	public int delete(String checkIds) {
		// TODO Auto-generated method stub
		for (String id : checkIds.split(", ")) {
			
			b_T_Ufp_TaskHandoutMapper.deleteByPrimaryKey(id);
		}
		return 1;
	}

	public int count(B_T_Ufp_TaskHandoutExample example,
			B_T_Ufp_TaskHandoutMapper b_T_Ufp_TaskHandoutMapper) {
		// TODO Auto-generated method stub
		return b_T_Ufp_TaskHandoutMapper.countByExample(example);
	}

}
